<?php

error_reporting(0);
function getTopDomainhuo(){
		$host=$_SERVER['HTTP_HOST'];
		
		$matchstr="[^\.]+\.(?:(".$str.")|\w{2}|((".$str.")\.\w{2}))$";
		if(preg_match("/".$matchstr."/ies",$host,$matchs)){
			$domain=$matchs['0'];
		}else{
			$domain=$host;
		}
		return $domain;

}
$domain=getTopDomainhuo();

$real_domain='baidu.com'; //

$check_host = 'http://yz.totovip.cn/update.php';
$client_check = $check_host . '?a=client_check&u=' . $_SERVER['HTTP_HOST'];
$check_message = $check_host . '?a=check_message&u=' . $_SERVER['HTTP_HOST'];
$check_info=file_get_contents($client_check);
$message = file_get_contents($check_message);



if($check_info=='1'){
   echo '<font color=red>' . $message . '</font>';
   die;
}elseif($check_info=='2'){
   echo '<font color=red>' . $message . '</font>';
   die;
}elseif($check_info=='3'){
   echo '<font color=red>' . $message . '</font>';
   die;
}

if($check_info!=='0'){ // 
   if($domain!==$real_domain){
      echo '';
	  die;
   }
}

unset($domain);

include('./config.php');
require './json.class.php';
require './Application/Home/Common/function.php';
function unzipIpaFile($ipaFile)
{
	\PHP_TOT_OTAServer\unzip($ipaFile, "Temp/");
	return "Temp/";
}
//$_POST['uid'] = '4180';
//$_POST['iAppSize'] = '5000';
//$_POST['name'] = '2017-07-18/596dcb6a83fab.ipa';
//$_POST['iLocalId'] = '1186';

$iLocalId = $_POST['iLocalId'];
$uid = $_POST['uid'];
$iAppSize = $_POST['iAppSize'];
//$_GET['dafufile'] = '2017-07-12/nc.apk';
$dafufile = trim($_POST['name']);

$dafufilearr = explode("/",$dafufile);
$apkfilename = $dafufilearr[1];
$dafupng = explode(".",$dafufilearr[1]) ;
$ext = strtolower($dafupng[1]);

if($ext=='apk'){
	
	include('./Apkparser.php');
	$appObj  = new Apkparser(); 
	 
	$targetFile = "./Public/uploads/".$dafufile;
	$filepath = "./Public/uploads/".$dafufile;

	
	$res   = $appObj->open($targetFile);

	$rand = rand(1000,9999);
	$imagename = $dafupng[0];
	$imagename = $imagename.time().$rand;

	/***********dafu************/
	 
	$appname = $appObj->getAppName();     // 应用名称
	$apppackage = $appObj->getPackage();    // 应用包名
	$appversionname = $appObj->getVersionName();  // 版本名称
	$appcode = $appObj->getVersionCode();  // 版本代码

	$appObj->getActivity();

		require_once './util/Classes/TOTClasses/unzip.php';
		require_once './util/Classes/TOTClasses/HandleInfoPlistInPayload.php';
		require_once './util/Classes/TOTClasses/FileSystemHelper.php';
		$unzipPath = unzipIpaFile($targetFile);

		$ptb=$unzipPath."res/drawable-hdpi-v4/icon.png";
		$apicloudimg=$unzipPath."res/drawable-hdpi/uz_icon.png";
		$ptb2=$unzipPath."res/drawable-hdpi/icon.png";

		$aptb=$unzipPath."res/drawable-hdpi-v4/app_icon.png";
		$aapicloudimg=$unzipPath."res/drawable-hdpi/app_icon.png";
		$aptb2=$unzipPath."res/drawable-hdpi/app_icon.png";
		 
		 
		 
	  //print_r($iconpath);
	  
	  if (file_exists($ptb)){
		//  print_r('找到');
		$newFile="./Public/Icon/".$imagename.".png"; //新目录
		 copy($ptb,$newFile); //拷贝到新目录
		 $newFile = $imagename.".png";
		 
	  }elseif(file_exists($apicloudimg)){
		  
		  $newFile="./Public/Icon/".$imagename.".png"; //新目录
		   copy($apicloudimg,$newFile); //拷贝到新目录
		  $newFile = $imagename.".png";
	  }elseif(file_exists($ptb2)){
		  
		  $newFile="./Public/Icon/".$imagename.".png"; //新目录
		   copy($ptb2,$newFile); //拷贝到新目录
		  $newFile = $imagename.".png";
		  
	  }elseif(file_exists($aptb)){
		  
		  $newFile="./Public/Icon/".$imagename.".png"; //新目录
		   copy($aptb,$newFile); //拷贝到新目录
		  $newFile = $imagename.".png";
	  }elseif(file_exists($aapicloudimg)){
		  
		  $newFile="./Public/Icon/".$imagename.".png"; //新目录
		   copy($aapicloudimg,$newFile); //拷贝到新目录
		  $newFile = $imagename.".png";
		  
	  }elseif(file_exists($aptb2)){
		  
		  $newFile="./Public/Icon/".$imagename.".png"; //新目录
		   copy($aptb2,$newFile); //拷贝到新目录
		  $newFile = $imagename.".png";
		  
	  }else{
		  $newFile='dafuapk.png';
	  }
	  
	  
	  
		  \PHP_TOT_OTAServer\DeleteDir("Temp");
		//echo ($appname."<br>".$apppackage."<br>".$appversionname."<br>".$appcode."<br>".$dafufile."<br>".$newFile);

		$data = array();
		$data['flag'] = '0';
		
		$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
		$file = './Public/uploads/'.$dafufile;
		$mmfilenamearr = explode('/',$dafufile);
		$mmfilename = end($mmfilenamearr);
		$remote_file = '/public_html/upload/'.$mmfilename; // 注意：这里的"/test"指的是ftp根目录下的test文件夹，而非系统跟目录下的test
		$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
		 
		if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
			$flag = 'ok';
		} else {
			$flag = 'error';
		}
		ftp_close($conn_id);
		//echo "[".$flag."]";

		
		$con = mysql_connect("localhost",$dbuser,$dbpass);
		if (!$con){
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($dbname, $con);

		$shorturl = urlShort($iLocalId);

		$nickname = $appname;
		$fid = $uid;
		$web = '/a/'.$shorturl;
		$uptxt = $server_url.$apkfilename;
		$create_time = time();
		$ext = $ext;
		$zsize = $iAppSize;
		$zxlogo = $newFile;
		$shorturl = $shorturl;
		$mmtype = '1';
		$isgg = '2';
		mysql_query("update lc_list set nickname='".$nickname."', fid='".$fid."', web='".$web."', uptxt='".$uptxt."', create_time='".$create_time."', ext='".$ext."', zsize='".$zsize."', zxlogo='".$zxlogo."', shorturl='".$shorturl."', mmtype='".$mmtype."',isgg='".$isgg."' where id='".$iLocalId."'");

		mysql_close($con);
		$data['flag'] = '1';
		$data['id'] = $iLocalId;
		$json = new Services_JSON;
		echo $json->encode($data);
		exit;
}else{

		

		$data = array();
		$data['flag'] = '0';
		
		$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");
		$file = './Public/uploads/'.$dafufile;
		$mmfilenamearr = explode('/',$dafufile);
		$mmfilename = end($mmfilenamearr);
		$remote_file = '/public_html/upload/'.$mmfilename; // 注意：这里的"/test"指的是ftp根目录下的test文件夹，而非系统跟目录下的test
		$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
		 
		if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
			$flag = 'ok';
		} else {
			$flag = 'error';
		}
		ftp_close($conn_id);

		
		$con = mysql_connect("localhost",$dbuser,$dbpass);
		if (!$con){
			die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($dbname, $con);

		$shorturl = urlShort($iLocalId);

		$newFile='dafuapk.png';
		$appname = '';
		
		$hpackageName = '';
		$hversion = '1.0';
		$ctlist = array(
			'ssl_server' => 'http://'.$_SERVER['SERVER_NAME'].'/',
			'bundle_identifier' => $hpackageName,
			'title' => $appname,
			'cid' => $server_url.$apkfilename,
			'subtitle' => $hpackageName,
			'versionname' => $hversion,
		);
		//echo "<pre>";print_r($ctlist);exit;


			//$fp = fopen('./Public/appipa/'.$iLocalId.".plist","w+");
			//fwrite($fp,createplist($ctlist));
			//fclose($fp); 

			//$plist = 'itms-services://?action=download-manifest&url='.'https://sl.totovip.cn/Public/appipa/'.$iLocalId.".plist";



		$nickname = $appname;
		$fid = $uid;
		$web = '/a/'.$shorturl;
		$uptxt = $server_url.$apkfilename;
		$create_time = time();
		$ext = $ext;
		$zsize = $iAppSize;
		$zxlogo = $newFile;
		$shorturl = $shorturl;
		$mmtype = '1';
		$isgg = '2';
		mysql_query("update lc_list set nickname='".$nickname."', fid='".$fid."', web='".$web."', uptxt='".$uptxt."', create_time='".$create_time."', ext='".$ext."', zsize='".$zsize."', zxlogo='".$zxlogo."', shorturl='".$shorturl."', mmtype='".$mmtype."', isgg='".$isgg."' where id='".$iLocalId."'");

		mysql_close($con);
		$data['flag'] = '1';
		$data['id'] = $iLocalId;
		$json = new Services_JSON;
		echo $json->encode($data);
		exit;


}


?><?php 